//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hl.sfzy.controller;

import com.hl.sfzy.entity.Problem;
import com.hl.sfzy.entity.Reply;
import com.hl.sfzy.service.ReplyService;
import com.hl.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping({"/reply"})
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    public ReplyController() {
    }

    @PostMapping({"/add"})
    public Result add(@RequestBody Reply Reply) throws Exception {
        this.replyService.add(Reply);
        return Result.success();
    }

    @DeleteMapping({"/remove/{id}"})
    public Result remove(@PathVariable Integer id) {
        this.replyService.remove(id);
        return Result.success();
    }

    @DeleteMapping({"/remove"})
    public Result removeBatch(Integer[] ids) {
        System.out.println(ids);
        this.replyService.removeBatch(ids);
        return Result.success();
    }

    @GetMapping({"/find/{id}"})
    public Result findById(@PathVariable Integer id) {
        return Result.success(this.replyService.findById(id));
    }

    @GetMapping({"/find"})
    public Result findPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize, Reply reply) {
        System.out.println(reply);
        return Result.success(this.replyService.findPage(pageNum, pageSize, reply));
    }

    @GetMapping({"/findByProblemId"})
    public Result findByProblemId(Integer problemid){
        List<Reply> replyList = replyService.selectByProblemId(problemid);
        return Result.success(replyList);
    }

    /**
     * 查回答
     * @param userId 用户id
     * @return 回答id和回答内容
     */
    @GetMapping({"/selectByUserId"})
    public Result selectByUserId(Integer userId){
        List<Reply> replies = replyService.selectByUserId(userId);
        return Result.success(replies);
    }

    /**
     * 查问题
     * @param replyId 回答id
     * @return 问题题目和问题的具体内容
     */
    @GetMapping({"/selectByReplyId"})
    public Result selectByReplyId(Integer replyId){
        Problem problem = replyService.selectByReplyId(replyId);
        return Result.success(problem);
    }
}
